package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.SearchTopicEntity;
import com.zw.yzk.kabu.data.network.task.SearchTopicTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.mapper.SearchTopicMapper;
import com.zw.yzk.kabu.view.view.SearchTopicView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/5/14.
 */

public class SearchTopicPresenter implements Presenter {

    private UseCase searchTopic;
    private AppCompatActivity activity;
    private SearchTopicMapper mapper;
    private SearchTopicView view;

    @Inject
    public SearchTopicPresenter(UseCase searchTopic, AppCompatActivity activity, SearchTopicMapper mapper) {
        this.searchTopic = searchTopic;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(SearchTopicView v) {
        this.view = v;
    }

    public void searchTopic(int start, String key) {
        searchTopic.execute(new SearchTopicSubscriber(activity), new SearchTopicTask(start, key));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        searchTopic.unSubscribe();
    }

    private class SearchTopicSubscriber extends BaseSubscriber<SearchTopicEntity> {

        public SearchTopicSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(SearchTopicEntity entity) {
            super.onNext(entity);
            view.searchTopicSuccess(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.searchTopicFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.search_exhibition_error);
        }
    }
}
